var express = require('express');
const myroute = express.Router()
// const multer = require('multer')
const upload = require('../utils/multer')
const mySql = require('../model/index')
const up = ({ dest: __dirname + '../public/images/avatar' })
// 上传图片
myroute.post('/admin/api/upload', upload.single('avatar'), async (req, res) => {
    console.log(req.file);
    const file = req.file
    file.url = 'http://192.168.1.44:8787' + '/images/' + `${file.filename}`
    console.log(file.url);
    res.send(file.filename)
    let newAvatar = req.file.filename;  //获取存放完成的新的文件名
    let user = req.body;
    // res.send(newAvatar)
    // console.log(req.file.filename);
    // const sql = `UPDATE admin SET figure='${req.file.filename}' WHERE work_id ='${req.body.username}'`
    // mySql.query(sql, function (err, data) {
    //     // console.log(err, data);
    //     if (err) {
    //         return res.json({
    //             code: 500,
    //             msg: '上传失败'
    //         })
    //     }
    //     res.json({
    //         code: 200,
    //         msg: '上传成功',
    //         data: data
    //     })
    // })
})

// 获取meeting表数据
myroute.get('/meeting/get', function (request, response) {
    const sql = `SELECT * FROM meeting JOIN meetingroom ON meeting.meeting_room=meetingroom.metroom_id`
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '查询会议表失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '查询会议表成功',
            data: data
        })
    })
})
// 获取meeting表数据
myroute.post('/meetingwhere/get', function (request, response) {
    const sql = `SELECT * FROM meeting JOIN meetingroom ON meeting.meeting_room=meetingroom.metroom_id WHERE meeting_per ='${request.body.meeting_per}'`
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '查询会议表失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '查询会议表成功',
            data: data
        })
    })
})
// 获取考勤
myroute.get('/kq', function (request, response) {
    const sql = `SELECT * FROM application join application_type on application.type=application_type.id`
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '查询失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '查询成功',
            data: data
        })
    })
})
// 获取meetingroom表数据
myroute.get('/meetingroom/get', function (request, response) {
    const sql = `SELECT * FROM 
(SELECT  *,typee AS mb_type, equ_name AS mb  FROM equipment,meetingroom WHERE equipment.equ_id=meetingroom.metroom_mb) AS mbtab JOIN 
(SELECT *,typee AS ht_type, equ_name AS ht  FROM equipment,meetingroom WHERE equipment.equ_id=meetingroom.metroom_ht) AS httab ON mbtab.metroom_id=httab.metroom_id JOIN
(SELECT *,typee AS tyy_type, equ_name AS tyy  FROM equipment,meetingroom WHERE equipment.equ_id=meetingroom.metroom_tyy) AS tyytab ON tyytab.metroom_id=httab.metroom_id`
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '查询会议室失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '查询会议室成功',
            data: data
        })
    })
})
// 查询会议室的内容
myroute.get('/meetingroomdetail/get', function (request, response) {
    const sql = `SELECT * FROM 
(SELECT  *,typee AS mb_type, equ_name AS mb  FROM equipment,meetingroom WHERE equipment.equ_id=meetingroom.metroom_mb) AS mbtab JOIN 
(SELECT *,typee AS ht_type, equ_name AS ht  FROM equipment,meetingroom WHERE equipment.equ_id=meetingroom.metroom_ht) AS httab ON mbtab.metroom_id=httab.metroom_id JOIN
(SELECT *,typee AS tyy_type, equ_name AS tyy  FROM equipment,meetingroom WHERE equipment.equ_id=meetingroom.metroom_tyy) AS tyytab ON tyytab.metroom_id=httab.metroom_id WHERE mbtab.metroom_id='${request.query.meetroom_id}'`
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '查询会议室内容失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '查询会议室内容成功',
            data: data
        })
    })
})
// 获取equipment表数据
myroute.get('/equipment/get', function (request, response) {
    const sql = `SELECT * FROM equipment`
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '查询设备失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '查询设备成功',
            data: data
        })
    })
})
// 获取admin表数据
myroute.post('/admin/get', function (request, response) {
    const sql = `SELECT * FROM admin JOIN postadmin ON jobs=post_id  JOIN department ON post_department=department.id WHERE work_id ='${request.body.userid}' `
    mySql.query(sql, function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '查询员工资料失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '查询员工资料成功',
            data: data
        })
    })
})
// 修改资料
myroute.post('/admin/edi', function (request, response) {
    console.log(request.body);
    console.log(request.body.address);
    const sql = `UPDATE admin SET name='${request.body.name}',type='${request.body.type}',time='${request.body.time}',jobs=${request.body.jobs},sex='${request.body.sex}',phone='${request.body.phone}',email='${request.body.email}',address='${request.body.address}',figure='${request.body.figure}' WHERE work_id=${request.body.work_id}`
    // const sql2 = `UPDATE meeting SET meeting_num=?,meeting_tit=?,meeting_per=?,meeting_state=? WHERE work_id=? `
    mySql.query(sql, function (err, data) {
        console.log(sql);
        if (err) {
            return response.json({
                code: 500,
                msg: '修改资料失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '修改资料成功',
            data: data
        })
    })
})
// 修改会议内容
myroute.post('/meeting/edi', function (request, response) {
    const sql = `UPDATE meeting SET meeting_num=?,meeting_tit=?,meeting_per=?,meeting_state=? WHERE meeting_id=? `
    mySql.query(sql, [request.body.meeting_num, request.body.meeting_tit, request.body.meeting_per, request.body.meeting_state, request.body.meeting_id], function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '修改会议内容失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '修改会议内容成功',
            data: data
        })
    })
})
// 删除会议内容
myroute.post('/meeting/del', function (request, response) {
    const sql = `delete from meeting WHERE meeting_id=? `
    mySql.query(sql, [request.body.id], function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '删除会议内容失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '删除会议内容成功',
            data: data
        })
    })
})
// 修改会议室内容
myroute.post('/meetingroom/edi', function (request, response) {
    const sql = `UPDATE meetingroom SET metroom_name=?,metroom_standard=?,metroom_capacity=?,metroom_status=?,metroom_mb=?,metroom_ht=?,metroom_tyy=? WHERE metroom_id=? `
    mySql.query(sql, [request.body.metroom_name, request.body.metroom_standard, request.body.metroom_capacity, request.body.metroom_status, request.body.metroom_mb, request.body.metroom_ht, request.body.metroom_tyy, request.body.metroom_id], function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '修改会议内容失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '修改会议内容成功',
            data: data
        })
    })
})
// 添加会议室
myroute.post('/meetingroom/add', function (request, response) {
    const sql = `INSERT INTO meetingroom VALUE (NULL,?,?,?,?,?,?,?)`
    mySql.query(sql, [request.body.metroom_name, request.body.metroom_standard, request.body.metroom_capacity, request.body.metroom_status, request.body.metroom_mb, request.body.metroom_ht, request.body.metroom_tyy], function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '添加会议室失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '添加会议室成功',
            data: data
        })
    })
})
// 修改设备内容
myroute.post('/equipment/edi', function (request, response) {
    const sql = `UPDATE equipment SET equ_name=?,equ_type=?,equ_status=?,equ_person=?,equ_todotime=?,typee=? WHERE equ_id=? `
    mySql.query(sql, [request.body.equ_name, request.body.equ_type, request.body.equ_status, request.body.equ_person, request.body.equ_todotime, request.body.typee, request.body.equ_id], function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '修改设备失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '修改设备成功',
            data: data
        })
    })
})
// 添加设备
myroute.post('/equipment/add', function (request, response) {
    const sql = `INSERT INTO equipment VALUE (NULL,?,?,?,?,?,?)`
    mySql.query(sql, [request.body.equ_name, request.body.equ_type, request.body.equ_status, request.body.equ_person, request.body.equ_todotime, request.body.typee], function (err, data) {
        // console.log(err, data);
        if (err) {
            return response.json({
                code: 500,
                msg: '添加会议室失败，请稍后重试'
            })
        }
        response.json({
            code: 200,
            msg: '添加会议室成功',
            data: data
        })
    })
})
module.exports = myroute